package com.tarim.wheat.controller;

import com.tarim.wheat.model.Type;
import com.tarim.wheat.service.TypeService;
import com.tarim.wheat.util.Constants;
import com.tarim.wheat.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@CrossOrigin
@Controller
@RequestMapping("/type")
public class TypeController {
    @Autowired
    TypeService typeService;

    /**
     * 获取全部分类
     * @return
     */
    @RequestMapping("/getAll")
    @ResponseBody
    public JsonResult selectAll(){
        JsonResult js;
        try {
            List list = typeService.selectAll();
            js = new JsonResult(Constants.STATUS_SUCCESS, "成功", list);
        }catch (Exception ex){
            js = new JsonResult(Constants.STATUS_ERROR, "异常");
        }
        return js;
    }

    /**
     * 分页查询
     * @param id
     * @param typeName
     * @return
     */
    @RequestMapping("/query")
    @ResponseBody
    public JsonResult query(@RequestParam(value = "id", defaultValue = "") Long id,
                            @RequestParam(value = "typeName", defaultValue = "") String typeName){
        JsonResult js;
        try {
            Type type = new Type();
            type.setId(id);
            type.setTypeName(typeName);
            List list = typeService.selectByCondition(type);
            if(list.size() > 0){
                js = new JsonResult(Constants.STATUS_SUCCESS, "成功", list);
            }else {
                js = new JsonResult(Constants.STATUS_NOT_FOUND, "获取列表失败");
            }
        }catch (Exception ex){
            ex.printStackTrace();
            js = new JsonResult(Constants.STATUS_ERROR, "异常");
        }
        return js;
    }
}
